package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author Login
 * @since 2025-07-17
 */
@Tag(name="品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private IBrandService brandService;

    /*
    * 获取品牌列表
    * */
    @Operation(summary = "品牌列表")
    @GetMapping(value = "list")
    public TableDataInfo list(@Parameter(description = "品牌名称") String name) {
        startPage();
        List<Brand> list = brandService.selectBrandList(name);
        return getDataTable(list);
    }

    @Operation(summary = "根据id获取详细信息")
    @GetMapping(value ="{ids}")
    public AjaxResult getInfo (@PathVariable("ids") long id){
        return success(brandService.getById(id));
    }

    @Operation(summary = "新增品牌")
    @PostMapping
    public AjaxResult add(@RequestBody Brand brand){
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.save(brand));
    }

    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult update(@Parameter(description = "品牌") @RequestBody Brand brand){
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateById(brand));
    }
    @Operation(summary = "删除品牌")  // 1. Swagger注解：API文档说明
    @DeleteMapping("/{ids}")         // 2. 定义HTTP DELETE请求映射
    public AjaxResult remove(        // 3. 方法声明：返回统一响应格式
         @Parameter(description = "品牌id列表") // 4. 参数文档说明
         @PathVariable List<Long> ids) { // 5. 接收路径参数
        return toAjax(brandService.removeBatchByIds(ids)); // 6. 调用服务层执行删除
    }
    @Operation(summary = "获取全部品牌")
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll() {
        return success(brandService.list());
    }
}
